<!--
 * @Description: 商家寝室推广 - 批次组列表
 * @Author: liyujie
 * @Date: 2021/2/23
 * @Last Modified by: liyujie
 * @Last Modified time: 2021/04/09
-->
<template>
    <div class="wrapper">
        <div class="content-wrapper">
            <!-- header -->
            <div class="header">
                <div class="header-title">
                    批次组列表
                </div>
                <div class="add-btn">
                    <el-button
                        type="primary"
                        size="medium"
                        :class="multipleSelection.length ? '' : 'is-disabled'"
                        @click="confirmMerge"
                    >
                        确认合并
                    </el-button>
                    <el-button
                        type="primary"
                        size="medium"
                        icon="el-icon-plus"
                        @click="$refs.newBatch.show('add')"
                    >
                        新建推广批次
                    </el-button>
                </div>
            </div>
            <!-- /header -->
            <!-- 筛选-->
            <div class="filter-view">
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        批次组名称
                    </p>
                    <el-input
                        placeholder="请输入批次组名称"
                        v-model="filterData.title"
                        suffix-icon="el-icon-search"
                        clearable
                        size="medium"
                        style="width: 286px"
                        @keydown.enter.native="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        推广状态
                    </p>
                    <el-select
                        v-model="filterData.status"
                        filterable
                        placeholder="全部"
                        size="medium"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in statusList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        视频上传来源
                    </p>
                    <el-select
                        v-model="filterData.type"
                        filterable
                        placeholder="全部"
                        size="medium"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in typeList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        计划推广时间
                    </p>
                    <el-date-picker
                        v-model="dateTimeRange"
                        type="datetimerange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                        size="medium"
                        style="width: 400px"
                        value-format="timestamp"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">
                        创建时间
                    </p>
                    <el-date-picker
                        v-model="createTimeRange"
                        type="datetimerange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                        size="medium"
                        style="width: 400px"
                        value-format="timestamp"
                    />
                </div>
                <div class="header-new-box">
                    <el-button
                        type="primary"
                        size="small"
                        @click="getTableData(1)"
                    >
                        查询
                    </el-button>
                </div>
            </div>
            <!--筛选-->

            <!-- table -->
            <el-table
                :data="tableData"
                height="100%"
                style="width: 100%;"
                ref="tempLateName"
                class="data-table"
                row-key="series_no"
                v-loading="tableLoading"
                @selection-change="handleSelectionChange"
            >
                <el-table-column
                    align="center"
                    width="50"
                    type="selection"
                    reserve-selection
                />
                <el-table-column
                    label="序号"
                    align="center"
                    width="80"
                    type="index"
                />
                <el-table-column
                    label="批次组名称"
                    show-overflow-tooltip
                    align="left"
                    width="260"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <div class="title-wrap">
                            <div class="series-title">{{ scope.row.title }}</div>
                            <span class="title-spliter" />
                            <el-button
                                type="text"
                                class="btn-title"
                                @click="showEditDialog(scope.row)"
                            >
                                <i class="el-icon-edit" />
                            </el-button>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="count"
                    label="寝室推广数量"
                    show-overflow-tooltip
                    align="center"
                    width="110"
                />
                <el-table-column
                    prop="school_name"
                    label="学校"
                    show-overflow-tooltip
                    align="center"
                    :formatter="emptyFormatter"
                />
                <el-table-column
                    label="寝室性别"
                    show-overflow-tooltip
                    align="center"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        {{ scope.row.sex === 1 ? '男' : scope.row.sex === 2 ? '女' : '不限' }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="计划推广时间"
                    align="center"
                    :formatter="emptyFormatter"
                    width="200"
                >
                    <template slot-scope="scope">
                        <div v-if="scope.row.schedule_list && scope.row.schedule_list.length === 0">--</div>
                        <div v-if="scope.row.schedule_list.length >= 1">
                            {{ scope.row.schedule_list[0]['start_time'] * 1000 | dateFormatter('yyyy/MM/dd') }} - {{ scope.row.schedule_list[0]['end_time'] * 1000 | dateFormatter('yyyy/MM/dd') }}
                        </div>
                        <div v-if="scope.row.schedule_list.length === 2">
                            <div>{{ scope.row.schedule_list[1]['start_time'] * 1000 | dateFormatter('yyyy/MM/dd') }} - {{ scope.row.schedule_list[1]['end_time'] * 1000 | dateFormatter('yyyy/MM/dd') }}</div>
                        </div>
                        <div v-if="scope.row.schedule_list.length > 2">
                            <el-popover
                                placement="bottom"
                                width="246"
                                trigger="click"
                                popper-class="date-list-popper"
                            >
                                <div
                                    v-for="(row,index) in scope.row.schedule_list.slice(1)"
                                    :key="index"
                                    class="date-item"
                                >
                                    {{ index + 1 }}、{{ row['start_time'] * 1000 | dateFormatter('yyyy/MM/dd') }} - {{ row['end_time'] * 1000 | dateFormatter('yyyy/MM/dd') }}
                                </div>
                                <el-button
                                    slot="reference"
                                    type="text"
                                >
                                    查看更多
                                </el-button>
                            </el-popover>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="是否委派"
                    show-overflow-tooltip
                    align="center"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <div v-if="scope.row.appoint">是</div>
                        <div v-else>否</div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="创建时间"
                    show-overflow-tooltip
                    align="center"
                    width="160"
                >
                    <template slot-scope="scope">
                        {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm:ss') }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="推广状态"
                    show-overflow-tooltip
                    align="center"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        {{ scope.row.status | statusFilter }}
                    </template>
                </el-table-column>
                <el-table-column
                    label="视频上传来源"
                    show-overflow-tooltip
                    align="center"
                    width="110"
                    :formatter="emptyFormatter"
                >
                    <template slot-scope="scope">
                        <div v-if="scope.row.type === 1">平台上传</div>
                        <div v-else-if="scope.row.type === 2">用户上传</div>
                        <div v-else>--</div>
                    </template>
                </el-table-column>
                <el-table-column
                    label="操作"
                    align="left"
                    width="200"
                    fixed="right"
                >
                    <template slot-scope="scope">
                        <div class="option-btn-group">
                            <el-button
                                type="text"
                                size="medium"
                                @click="handleRoute('dormExtensionDetail', {no: scope.row.series_no})"
                            >
                                查看
                            </el-button>
                            <el-button
                                type="text"
                                size="medium"
                                @click="handleEdit(scope.row)"
                            >
                                修改
                            </el-button>
                            <el-button
                                type="text"
                                size="medium"
                                @click="handleRoute('dormExtensionEdit', {no: scope.row.series_no})"
                            >
                                编辑进度
                            </el-button>
                        </div>
                    </template>
                </el-table-column>
            </el-table>
            <!-- /table -->

            <!-- /分页 -->
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    layout="total, prev, pager, next, jumper"
                    background
                    :current-page.sync="page.now_page"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData"
                />
            </div>
        </div>
        <!--新建批次组弹窗-->
        <new-batch-dialog
            ref="newBatch"
            @update="getTableData(1)"
        />
        <!--修改批次组名称-->
        <series-title-edit
            ref="editTitle"
            @confirm="setSeriesTitle(series_no, $event)"
        />
        <!--合并批次组-->
        <merge-series-group
            ref="mergeSeries"
            @update="getTableData(1)"
            @close="$refs.tempLateName.clearSelection()"
        />
    </div>
</template>

<script>
import { back } from '@/mixin/back';
import NewBatchDialog from "@/components/dorm/new-batch-dialog";
import SeriesTitleEdit from "@/components/dorm/series-title-edit";
import MergeSeriesGroup from "@/components/dorm/merge-series-group";
import { deepClone } from "@/assets/js/utils";

export default {
    name: 'DormExtensionList',
    mixins: [back],
    components: {MergeSeriesGroup, SeriesTitleEdit, NewBatchDialog},
    data() {
        return {
            filterData: {
                type: 99,
                title: '',
                status: 99,
                task_schedule_start_start_time: '',
                task_schedule_start_end_time: '',
                create_start_time: '',
                create_end_time: '',
            },
            dateTimeRange: [],
            createTimeRange: [],
            tableData: [],
            loading: false,
            tableLoading: false,
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 20
            },
            series_no: '',
            statusList: [
                {label: '全部', value: 99},
                {label: '排期中', value: 1},
                {label: '排队中', value: 2},
                {label: '进行中', value: 3},
                {label: '已完成', value: 4},
                {label: '已终止', value: 5},
                {label: '已暂停', value: 6},
            ],
            typeList: [
                {label: '全部', value: 99},
                {label: '用户上传', value: 2},
                {label: '平台上传', value: 1},
            ],
            // 表格选择数据
            multipleSelection: [],
            authResult: {}
        };
    },
    computed: {},
    filters: {
        statusFilter(val) {
            switch (val) {
                case 1:
                    return '排期中';
                case 2:
                    return '排队中';
                case 3:
                    return '进行中';
                case 4:
                    return '已完成';
                case 5:
                    return '已终止';
                case 6:
                    return '已暂停';
                default:
                    return '--';
            }
        },
    },
    watch: {
        dateTimeRange(value) {
            this.filterData.task_schedule_start_start_time = value ? value[0] / 1000 : '';
            this.filterData.task_schedule_start_end_time = value ? value[1] / 1000 : '';
            this.getTableData(1);
        },
        createTimeRange(value) {
            this.filterData.create_start_time = value ? value[0] / 1000 : '';
            this.filterData.create_end_time = value ? value[1] / 1000 : '';
            this.getTableData(1);
        }
    },

    mounted() {
        this.$store.commit('SET_HEADER', [{title: "官网"}, {title: '商家增值服务'}, {title: '寝室视频推广进度'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },

    methods: {

        /**
         * @description 获取列表数据
         * @param page
         */
        getTableData(page) {
            this.tableLoading = true;
            let params = { ...this.filterData };

            params.page_id = page || this.page.now_page;
            this.$post('/student/room_series_task%5Cget_list', params, res => {
                if (res.code === 1) {
                    this.tableData = res.data.list;
                    this.page = res.data.page;
                } else {
                    this.$notify({
                        title: '提示',
                        type: 'warning',
                        message: res.msg
                    });
                }
                this.tableLoading = false;
            });
        },

        /**
         * 路由跳转
         * @param routeName
         * @param query
         */
        handleRoute(routeName, query) {
            this.$router.push({
                name: routeName,
                query
            });
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 编辑批次
         * @param row
         * @returns {boolean}
         */
        handleEdit(row) {
            if(!this.authResult.auth_edit) {
                this.$notify({
                    title: '提示',
                    message: '请联系管理员添加权限',
                    type: 'warning',
                    duration: 2000
                });
                return false;
            }
            this.$post('/student/room_series_task%5Cget', {series_no: row.series_no}, resp => {
                if (resp.code === 1) {
                    let data = {...resp.data, series_no: row.series_no};

                    this.$refs.newBatch.show('edit', data);
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

        /**
         * 显示批次组名称修改弹窗
         * @param row
         * @param type
         */
        showEditDialog(row, type) {
            this.series_no = row.series_no;
            this.$refs.editTitle.show('修改批次组名称', row.title);
        },

        /**
         * 设置批次组名称
         * @param series_no
         * @param title
         */
        setSeriesTitle(series_no, title) {
            let params = {
                series_no,
                title
            };

            this.$post('/student/room_series%5Cupdate', params, resp => {
                if (resp.code === 1) {
                    this.getTableData();
                    this.$notify({
                        title: '提示',
                        message: '操作成功',
                        type: 'success',
                        duration: 2000
                    });
                } else {
                    this.$notify({
                        title: '提示',
                        message: resp.msg,
                        type: 'warning',
                        duration: 2000
                    });
                }
            });
        },

        /**
         * 表格现在
         * @param val
         */
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },

        /**
         * 确认合并批次组
         */
        confirmMerge() {
            if(this.multipleSelection.length === 0) {
                this.$message.warning('未选中任何批次组');
                return;
            }
            this.$refs.mergeSeries.show(deepClone(this.multipleSelection));
        },
    }
};
</script>

<style lang="less" scoped>
.wrapper {
    width: 100%;

    .content-wrapper {
        display: flex;
        height: calc(100% - 24px);
        flex-direction: column;
        position: relative;
        margin: 12px;
        padding: 24px;
        background: #fff;
        box-sizing: border-box;

        /*header*/

        .header {
            box-sizing: border-box;
            display: flex;
            align-items: center;
            justify-content: space-between;
            border-bottom: 1px solid #F0F2F5;

            .header-title {
                color: #333333;
                font-size: 20px;
                height: 20px;
                font-weight: 500;
                position: relative;
                margin-left: 12px;
                margin-bottom: 24px;

                &::before {
                    content: '';
                    position: absolute;
                    left: -8px;
                    top: -1px;
                    width: 3px;
                    height: 20px;
                    background: #1890FF;
                }
            }
            .add-btn {
                margin-top: -16px;
            }
        }

        /*筛选*/

        .filter-view {
            margin-top: 12px;
            box-sizing: border-box;
            display: flex;
            align-items: center;
            flex-wrap: wrap;

            .header-new-box {
                display: flex;
                align-items: center;
                font-family: PingFangSC-Regular, PingFang SC;
                font-weight: 400;
                color: #333333;
                white-space: nowrap;
                margin-right: 24px;
                margin-bottom: 12px;

                &-title {
                    margin-right: 16px;
                    white-space: nowrap;
                }
            }
        }

        .title-wrap {
            display: flex;
            align-items: center;
            cursor: pointer;
            .series-title {
                max-width: 200px;
                text-overflow:ellipsis;
                white-space:nowrap;
                overflow: hidden;
            }
            .title-spliter {
                width: 1px;
                height: 12px;
                background: rgba(235, 235, 235, 1);
                margin: 0 10px;
            }
            .title-sort {
                font-size: 14px;
                line-height: 1;
                color: #ccc;
                &:hover {
                    color: #20a0ff;
                }
            }
        }

        // 表格
        .data-table {
            ::-webkit-scrollbar {
                width: 6px;
                height: 6px;
            }
            // 操作按钮
            .option-btn-group {
                .el-button {
                    margin-left: 0;
                    margin-right: 10px;
                }
            }
        }

        /* 分页 */

        .pagination-wrapper {
            padding-top: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /deep/ .el-dialog__header {
        padding-top: 10px;
        box-shadow:0 1px 0 0 rgba(233,233,233,1);
        display: flex;
        align-items: center;
        background: #FAFAFA;
        border-radius: 4px 4px 0 0;
        .el-dialog__title {
            font-weight: 500;
            color: #333;
            font-size: 14px;
        }
        .el-icon-close:before {
            color: rgb(194,194,194);
            font-size: 17px;
            font-weight: bold;
        }
        .el-dialog__headerbtn {
            top: 14px;
        }
    }
    /deep/ .el-textarea__inner {
        resize: none;
    }
}
</style>

<style lang="less">
.date-list-popper {
    padding: 15px 24px;
    display: flex;
    flex-direction: column;
    align-items: center;
    .date-item {
        margin: 6px 0;
    }
}
</style>
